/**
 *
 * @author javi
 */

package fmap.gui.jTreePersonal;

import fmap.DatosCompartidos;
import fmap.model.MapF;
import fmap.model.Collection;
import fmap.model.Node;
import javax.swing.event.TreeModelListener;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
import java.util.Vector;


public class TreeModelPersonal implements TreeModel
{
    private Vector treeModelListeners = new Vector();
    private Collection root;
    
    public TreeModelPersonal(Collection _root)
    {
        root = _root;
    }
    
    
//////////////// Fire events //////////////////////////////////////////////
    
    /**
     * The only event raised by this model is TreeStructureChanged with the
     * root as path, i.e. the whole tree has changed.
     */
/*
    protected void fireTreeStructureChanged(Person oldRoot) {
        int len = treeModelListeners.size();
        TreeModelEvent e = new TreeModelEvent(this,
                                              new Object[] {oldRoot});
        for (int i = 0; i < len; i++) {
            ((TreeModelListener)treeModelListeners.elementAt(i)).
                    treeStructureChanged(e);
        }
    }
*/
    
    
    
    protected Vector getChildren(Object node)
    {
        Vector v = new Vector();

        if( node instanceof Collection )
        {
            Collection col = (Collection)node;
            v = col.getVMapF ();
        }
        else if( node instanceof MapF )
        {
            MapF mapF = (MapF)node;
            
            if( mapF.getVNode ().size () > 0 )
                v = mapF.getVNodeType (0);
            else
            {
                v = new fmap.dao.NodeDao().selectNodesType ( mapF.getIdMapF (), -1, 0, mapF.getIdTable () );
                mapF.setVNode ( v );
            }
        }
        else if( node instanceof Node )
        {
            Node n = (Node)node;

            v = n.getVNode ();

            if ( v != null )
            {

                if ( v.size () > 0 )
                {
                    v = n.getVNodeType (0);
                }
                else
                {
                    v = new fmap.dao.NodeDao().selectNodesType ( n.getIdMapF (), n.getIdNode (), 0, n.getIdTableSub () );
                    n.setNode ( v );
                }

            }

//            v = d.getDir2 ();
        }

        return v;
    }
    
    
//////////////// TreeModel interface implementation ///////////////////////
    
    /**
     * Adds a listener for the TreeModelEvent posted after the tree changes.
     */
    public void addTreeModelListener(TreeModelListener l)
    {
        treeModelListeners.addElement(l);
    }
    

    /**
     * Returns the child of parent at index index in the parent's child array.
     */
    public Object getChild(Object parent, int index)
    {
        return getChildren(parent).get(index);
    }
    
    /**
     * Returns the number of children of parent.
     */
    public int getChildCount(Object parent)
    {
        Vector children = getChildren(parent);
        return (children == null) ? 0 : children.size();
    }
    
    /**
     * Returns the index of child in parent.
     */

    public int getIndexOfChild(Object parent, Object child)
    {
    /*
        Person p = (Person)parent;
        if (showAncestors)
        {
            int count = 0;
            Person father = p.getFather();
            if (father != null)
            {
                count++;
                if (father == child)
                {
                    return 0;
                }
            }
            if (p.getMother() != child)
            {
                return count;
            }
            return -1;
        }
        return p.getIndexOfChild((Person)child);
    */
        return 1;
//        if ( child instanceof fmap.model.Node )
//        {
//            Node n = (Node)child;
//            return n.getIdNode ();
//        }
        
//        return (super.getChildCount());
    }

    /**
     * Returns the root of the tree.
     */
    public Object getRoot()
    {
        return root;
    }
    
    /**
     * Returns true if node is a leaf.
     */
    public boolean isLeaf(Object node)
    {
        if( node instanceof Collection )    return false;
        /*
        else if( node instanceof Node )
        {
            Node n = (Node)node;
//            if( new fmap.actions.node.tieneHijosAction().tieneHijos ( n.getIdNode () ) )  
//                return false;
//            else
                return true;
        }
        else if( node instanceof MapF )
        {
            MapF mapF = (MapF)node;
            if( new fmap.actions.mapF.tieneHijosAction().tieneHijos ( mapF.getIdMapF ()) )  
                return false;
            else
                return true;
        }
        else                                return true;
*/return false;
    }
    
    /**
     * Removes a listener previously added with addTreeModelListener().
     */
    public void removeTreeModelListener(TreeModelListener l)
    {
        treeModelListeners.removeElement(l);
    }
    
    /**
     * Messaged when the user has altered the value for the item
     * identified by path to newValue.  Not used by this model.
     */
    public void valueForPathChanged(TreePath path, Object newValue)
    {
        System.out.println("*** valueForPathChanged : "
                + path + " --> " + newValue);
    }
}
